package com.opgea.crm.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.SalesCalendarVo;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.services.CalenderService;

@Controller
@RequestMapping("salesCalender")
public class CalenderController {

	@Autowired
	CalenderService calenderService;
	
	@RequestMapping(method=RequestMethod.GET,value="fillCalenderDropDown")
	public @ResponseBody
	Map<String,List<SetupsValue>> fillCalenderDropDown(String operation,@RequestParam(required=false) Integer type){
		Map<String, List<SetupsValue>> mlist=new HashMap<String, List<SetupsValue>>();
		if(operation.equalsIgnoreCase("type"))
		{
			mlist.put("data",this.calenderService.getTypes());
		}
		if(operation.equalsIgnoreCase("assignto")&& type!=null)
		{
			mlist.put("data", this.calenderService.getAssignTo(ApplicationConstant.SalesCalenderType.values()[type]));
		}
		return mlist;
	}
	
	@RequestMapping(method=RequestMethod.GET,value="fillCalenderGrid")
	public @ResponseBody
	Map<String,List<SalesCalendarVo>> fillCalenderGrid(@RequestParam(required=false) Long assignToId,
													   @RequestParam(required=false) Integer typeid,
													   @RequestParam(required=false) String pfrom,
				
													   @RequestParam(required=false) String pto){
		
		
		Map<String, List<SalesCalendarVo>> mp=new HashMap<String, List<SalesCalendarVo>>();
		if(typeid!=null){
		Date fromdt=DateUtil.stringToDate(pfrom);
		Date todt=DateUtil.stringToDate(pto);
		List<SalesCalendarVo> list=this.calenderService.showCalenderGrid(assignToId, fromdt, todt,
																		ApplicationConstant.SalesCalenderType.values()[typeid]);
		
		mp.put("data", list);
		}
		return mp;		
	}
}
